package com.unipay.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @BelongsProject: SSM
 * @Author: cmy
 * @CreateTime: 2023-01-22  10:42
 * @Description: TODO 向域对象共享数据
 */

@Controller
@RequestMapping("/scope")
public class ScopeController {

/*
    @RequestMapping("/")
    public String protal(){
        // 将逻辑视图返回：逻辑视图 加 配置文件中的“视图前缀、视图后缀” = 物理视图
        return "index";
    }
 */
    //1、使用ServletAPI向request域对象共享数据
    @RequestMapping("/servletAPI")
    public String servletAPI(HttpServletRequest httpServletRequest){
        httpServletRequest.setAttribute("testScope", "Hello, servletAPI!");
        return "success";
    }
    //2、使用ModelAndView向request域对象共享数据
    @RequestMapping("/modelAndView")
    public ModelAndView modelAndView(){
        /**
         * ModelAndView有Model和View的功能
         * Model主要用于向请求域共享数据
         * View主要用于设置视图，实现页面跳转
         */
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("testScope", "Hello, ModelAndView!");
        modelAndView.setViewName("success");
        return modelAndView;
    }
    //3、使用Model向request域对象共享数据
    @RequestMapping("/model")
    public String model(Model model){
        model.addAttribute("testScope", "Hello, Model!");
        return "success";
    }
    //4、使用map向request域对象共享数据
    @RequestMapping("/map")
    public String map(Map<String, Object> map){
        map.put("testScope", "Hello, Map!");
        return "success";
    }
    //5、使用ModelMap向request域对象共享数据
    @RequestMapping("/modelMap")
    public String modelMap(ModelMap modelMap){
        modelMap.addAttribute("testScope", "Hello ModelMap");
        return "success";
    }


    //TODO 7、向session域共享数据
    @RequestMapping("/session")
    public String session(HttpSession httpSession){
        httpSession.setAttribute("testSessionScope", "Hello session！");
        return "success";
    }
    //8、向application域共享数据
    @RequestMapping("/application")
    public String application(HttpSession httpSession){
        ServletContext servletContext = httpSession.getServletContext();
        servletContext.setAttribute("testApplication", "Hello Application");
        return "success";
    }


    //TODO 视图：转发视图InternalResourceView、重定向视图RedirectView
    @RequestMapping("/forward")
    public String forward(){
        //TODO forward: + “根目录后” 绝对请求路径
        return "forward:/scope/application";  // ## 转发到的视图，不能通过ThymeleafView来进行渲染
        // http://localhost:8080/springMVC/scope/forward
    }
    @RequestMapping("/redirect")
    public String redirect(){
        return "redirect:/scope/application";
        // http://localhost:8080/springMVC/application
    }
}
